package com.btshop.goods.controller;
import com.btshop.goods.pojo.Spec;
import com.btshop.goods.service.SpecService;
import com.github.pagehelper.PageInfo;
import com.util.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @ClassName: SpecController
 * @description: Spec 控制层接口定义类
 * @author: 李东
 * @Date: 2020-7-20
 */
@Api(value = "SpecController")
@Slf4j
@RestController
@RequestMapping("/spec")
@CrossOrigin
public class SpecController {

    @Autowired
    private SpecService specService;

    /**
     * 根据分类id查询规格参数
     * @param categoryId
     * @return
     */
    @ApiOperation(value = "Spec根据分类id查询规格参数",notes = "根据分类id查询规格参数",tags = {"SpecController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "categoryId", value = "分类id", required = true, dataType = "Long")
    })
    @GetMapping(value = "/category/{categoryId}" )
    public Result<List<Spec>> findCategoryId(@PathVariable("categoryId") Long categoryId){
        List<Spec> specs = specService.findByCategoryId(categoryId);
        return Result.buildSuccess("根据分类id查询规格参数成功！",specs);
    }

    /***
     * Spec分页条件搜索实现
     * @param spec
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "Spec条件分页查询",notes = "分页条件查询Spec方法详情",tags = {"SpecController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Spec对象",value = "传入JSON数据",required = false) Spec spec, @PathVariable  int page, @PathVariable  int size){
        log.info("SpecController findPage params , spec : {} , page : {} , size : {}",spec,page,size);
        //调用SpecService实现分页条件查询Spec
        PageInfo<Spec> pageInfo = specService.findPage(spec, page, size);
        return Result.buildSuccess("查询成功！", pageInfo);
    }

    /***
     * Spec分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "Spec分页查询",notes = "分页查询Spec方法详情",tags = {"SpecController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        log.info("SpecController findPage params , page : {} , size : {}",page,size);
        //调用SpecService实现分页查询Spec
        PageInfo<Spec> pageInfo = specService.findPage(page, size);
        return Result.buildSuccess("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param spec
     * @return
     */
    @ApiOperation(value = "Spec条件查询",notes = "条件查询Spec方法详情",tags = {"SpecController"})
    @PostMapping(value = "/search" )
    public Result<List<Spec>> findList(@RequestBody(required = false) @ApiParam(name = "Spec对象",value = "传入JSON数据",required = false) Spec spec){
        log.info("SpecController findList params , spec : {} ",spec);
        //调用SpecService实现条件查询Spec
        List<Spec> list = specService.findList(spec);
        return Result.buildSuccess("查询成功！", list);
    }

    /***
     * 根据主键删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Spec根据主键id删除",notes = "根据主键删除Spec方法详情",tags = {"SpecController"})
    @ApiImplicitParams({
                @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
    })
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable("id") Long id){
        log.info("SpecController delete params ,   id : {}   ",id);
        //调用SpecService实现根据主键删除
        specService.delete(id);
        return Result.buildSuccess("删除成功！");
    }

    /***
     * 修改Spec数据
     * @param spec
     * @param id
     * @return
     */
    @ApiOperation(value = "Spec根据id修改",notes = "根据ID修改Spec方法详情",tags = {"SpecController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
     })
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "Spec对象",value = "传入JSON数据",required = false) Spec spec,@PathVariable("id") Long id){
        log.info("SpecController update params ,  spec : {},  id : {}   ",spec,id);
        //设置主键值
        spec.setId(id);
        //调用SpecService实现修改Spec
        specService.update(spec);
        return Result.buildSuccess("修改成功！");
    }

    /***
     * 新增Spec数据
     * @param spec
     * @return
     */
    @ApiOperation(value = "Spec添加",notes = "添加Spec方法详情",tags = {"SpecController"})
    @PostMapping
    public Result add(@RequestBody  @ApiParam(name = "Spec对象",value = "传入JSON数据",required = true) Spec spec){
        log.info("SpecController add params , spec : {}",spec);
        //调用SpecService实现添加Spec
        specService.add(spec);
        return Result.buildSuccess("添加成功！");
    }

    /***
     * 根据ID查询Spec数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Spec根据id查询",notes = "根据ID查询Spec方法详情",tags = {"SpecController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name ="id", value = "主键", required = true, dataType = "Long")
            })
    @GetMapping("/{id}")
    public Result<Spec> findById(@PathVariable("id") Long id){
        log.info("SpecController findById params ,   id : {}   ",id);
        //调用SpecService实现根据主键查询Spec
        Spec spec = specService.findById(id);
        return Result.buildSuccess("查询成功！",spec);
    }

    /***
     * 查询Spec全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Spec",notes = "查询所Spec有方法详情",tags = {"SpecController"})
    @GetMapping
    public Result<List<Spec>> findAll(){
        //调用SpecService实现查询所有Spec
        List<Spec> list = specService.findAll();
        return Result.buildSuccess("查询成功！",list);
    }
}
